package com.itheima.utils;

import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.dysmsapi20170525.models.SendSmsResponseBody;
import com.aliyun.teaopenapi.models.Config;

public class AliSMSUtil {
    /**
        本工具类所依赖的坐标
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>dysmsapi20170525</artifactId>
            <version>2.0.4</version>
        </dependency>
    */
    private static final String accessKeyId = "LTAI5tPsknuv7HHku7wbrSqm";
    private static final String accessKeySecret = "6d5x3CPoPylYbOFvThhg1haSLYxAZN";
    private static final String signName = "怪物史莱姆";
    private static final String templateCode_verificationCode = "SMS_199216427";
    private static final String templateCode_shortMessage = "SMS_199795039";

    private static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        Config config = new Config()
                // 您的AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 您的AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }

    public static SendSmsResponseBody sendMessageByAliSMS(String phoneNumber, String verificationCode) {
        try {
            com.aliyun.dysmsapi20170525.Client client = createClient(accessKeyId, accessKeySecret);
            SendSmsRequest sendSmsRequest = new SendSmsRequest()
                    .setPhoneNumbers(phoneNumber)
                    .setSignName(signName)
                    .setTemplateCode(templateCode_verificationCode)
                    .setTemplateParam("{\"code\":\"" + verificationCode + "\"}");
            System.out.println("{\"code\":\"" + verificationCode + "\"}");
            // 复制代码运行请自行打印 API 的返回值
            SendSmsResponse smsResponse = client.sendSms(sendSmsRequest);
            return smsResponse.getBody();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("发生了意外的错误");
        }
    }
}
